
# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import urllib
from datetime import datetime

def get_historical_dividends(symbol, start_date, end_date):
    url = 'http://www.dividendinvestor.co.uk/historical.php?exg=GB&symbol=%s' % symbol
    dataTable = urllib.urlopen(url).read().strip()
    dataTable = dataTable[dataTable.index('DCDCDC'):]
    dataTable = dataTable[:dataTable.index('</table>')]
    dataTable = dataTable.replace('DCDCDC" class="hyper13">','')
    dataTable = dataTable.replace('<b>','')
    dataTable = dataTable.replace('</b>','')
    dataTable = dataTable.replace('<div align="center">','')
    dataTable = dataTable.replace('</div>','')
    dataTable = dataTable.replace('\t','')
    dataTable = dataTable.replace('\n','')
    dataTable = dataTable.replace('</td>','')
    
    header = dataTable[:dataTable.index('</tr>')]
    header = header.replace('<td>','',1)
    header = header.split('<td>')
    header = [header[idx].strip() for idx in range(len(header))]
    dateIndex = header.index('Payable Date')
    priceIndex = header.index('Dividend Amount')

    datas = dataTable[dataTable.index('<tr>'):]
    datas = datas.replace('<tr>','',1)
    datas = datas.replace('</tr>','')
    divPayments = datas.split('<tr>')
    returnData = []
    previousDate = datetime.date(datetime(1001,1,1))
    for divPayment in divPayments:
        divPayment = divPayment.replace('<td>','',1)
        divPayment = divPayment.split('<td>')
        divPayment = [divPayment[idx].strip() for idx in range(len(divPayment))]
        if not divPayment[dateIndex] == '':
            try:
                date = datetime.date(datetime.strptime(divPayment[dateIndex],'%b-%d-%Y'))
                if date >= start_date and date <= end_date:
                    if date == previousDate:
                        returnData[-1][1] += float(divPayment[priceIndex])
                    else:
                        returnData.append([date, float(divPayment[priceIndex])])
                    previousDate = date
            except:
                pass

    return returnData